#!coding=utf-8
from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField
from weixin.models import User
from django.dispatch.dispatcher import receiver
from weixin.send.tempmsg import sendlogisticsmsgtocust, sendpayinfotocust,\
    sendpaycardinfotocust

class CustAdr(models.Model):
    """
             客户常用寄件地址信息
    """
    wx_id=models.CharField(u'微信id',max_length=30)
    custname=models.CharField(u'姓名',max_length=30)
    phoneno=models.CharField(u'联系方式',max_length=30)
    cityname=models.CharField(u'省市区名称',max_length=50)
    detailAdrinfo=models.CharField(u'具体地址信息',max_length=100)
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable
    class Meta:
        verbose_name = u"客户收货地址"
        verbose_name_plural = '客户收货地址管理'


class SaleInfo(models.Model):
    """
             促销信息表
    """
    salename=models.CharField(u'活动名称',max_length=30)
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable
    def __unicode__(self):
        return '%s' %self.salename
    class Meta:
        verbose_name = u"促销信息"
        verbose_name_plural = '促销信息管理'
        
class Catagory_T(models.Model):
    """
    大商品类别
    """    
    
    name=models.CharField(u'类别名称',max_length=20)
    postion=models.IntegerField(u'类别顺序')
    desc=models.CharField(u'描述',max_length=50,null=True,blank=True)
    modified=models.DateTimeField(u'更新时间',auto_now=True)
    def __unicode__(self):
        return '%s' %self.name
    class Meta:
        verbose_name = u'大商品类别'
        verbose_name_plural = '大商品类别管理'
    
class Catagory(models.Model):
    """
    商品类别
    """
    name=models.CharField(u'类别名称',max_length=20)
    postion=models.IntegerField(u'类别顺序')
    desc=models.CharField(u'描述',max_length=50,null=True,blank=True)
    catagory_T=models.ForeignKey(Catagory_T,verbose_name=u'大商品类别',blank=True,null=True)
    modified=models.DateTimeField(u'更新时间',auto_now=True)
    def __unicode__(self):
        return '%s' %self.name
    class Meta:
        verbose_name = u'商品类别'
        verbose_name_plural = '商品类别管理'
    
class Brand(models.Model):
    """
    品牌
    """
    name=models.CharField(u'品牌名称',max_length=20)
    postion=models.IntegerField(u'类别顺序')
    desc=models.CharField(u'描述',max_length=50,null=True,blank=True)
    modified=models.DateTimeField(u'更新时间',auto_now=True)
    def __unicode__(self):
        return '%s' %self.name
    class Meta:
        verbose_name = u'品牌'
        verbose_name_plural = '品牌管理'
class Goods(models.Model):
    """
    商品信息
    """
    catagory=models.ForeignKey(Catagory,verbose_name=u'商品类别')
    name=models.CharField(u'商品名称',max_length=60)
    brand=models.ForeignKey(Brand,verbose_name=u'品牌')
    saleinfo=models.ForeignKey(SaleInfo,verbose_name=u'促销信息',null=True,blank=True)
    price=models.DecimalField(u'商品价格',max_digits=7,decimal_places=2)
    desc=RichTextUploadingField(u'手机端的商品描述',max_length=1500,null=True,blank=True)
    web_desc=RichTextUploadingField(u'电脑web端的商品描述',max_length=1500,null=True,blank=True)
    postion=models.IntegerField(u'排序',null=True,blank=True)
    image_thumb=models.ImageField(u'缩略图地址',upload_to='uploads/tubiao/',null=True,blank=True)
    image1=models.ImageField(u'图片地址1',upload_to='uploads/image/',null=True,blank=True)
    image2=models.ImageField(u'图片地址2',upload_to='uploads/image/',null=True,blank=True)
    image3=models.ImageField(u'图片地址3',upload_to='uploads/image/',null=True,blank=True)
    image4=models.ImageField(u'图片地址4',upload_to='uploads/image/',null=True,blank=True)
    image5=models.ImageField(u'图片地址5',upload_to='uploads/image/',null=True,blank=True)
    score=models.IntegerField(u'积分',help_text=u'商品购买后的积分',null=True)
    is_promote=models.BooleanField(u'是否为推荐商品',default=False)
    star=models.IntegerField(u'几星级评价',null=True,default=3)
    sales=models.IntegerField(u'销量',default=0)
    stock=models.IntegerField(u'库存',default=0)
    is_book=models.BooleanField(u'是否接受预定')
    is_new=models.BooleanField(u'是否是新品')
    fencount = models.IntegerField(u'算几份',default=1)
    remark = models.CharField(u'备注信息',max_length=300,null=True,blank=True)
    modified=models.DateTimeField(u'更新时间',auto_now=True)
    def __unicode__(self):
        return '%s' %self.name
    class Meta:
        verbose_name = u'商品信息'
        verbose_name_plural = '商品信息管理'
    
class Order(models.Model):
    """
    订单
    """
    ORDER_STATUS=(
        ('已下单', u'已下单'),
        ('已取消', u'已取消'),
        ('已支付', u'已支付'),
        ('已发货', u'已发货'),
        ('收货', u'收货'),
        ('确认收货', u'确认收货'),
    )
    open_id=models.CharField(max_length=100) #需要加索引
    create_at=models.DateTimeField(u'创建时间',auto_now_add=True)
    customer_remark=models.CharField(u'客户留言',max_length=100,null=True,blank=True)
    status=models.CharField(u'订单状态',choices=ORDER_STATUS,default=u'已下单',max_length=100)
    order_remark=models.CharField(u'订单备注',max_length=100,null=True,blank=True)
    price=models.DecimalField(u'价格',max_digits=7,decimal_places=2,default=0)
    act_price=models.DecimalField(u'实际支付价格',max_digits=7,decimal_places=2,default=0)
    count=models.IntegerField(u'商品数量',help_text=u'每份订单数量的总和',null=True,default=0)
    comment=models.TextField(verbose_name=u'评论',blank=True,default='')
    
    logistics=models.CharField(u'物流公司名称',max_length=100,null=True,blank=True,default='')
    logistics_sts=models.CharField(u'物流状态',max_length=30,null=True,blank=True,default='')
    logistics_order=models.CharField(u'物流单号',max_length=100,null=True,blank=True,default='')
    logistics_price=models.DecimalField(u'物流费用',max_digits=7,decimal_places=2,default=0)
    logistics_send_at = models.DateTimeField(u'物流发送时间',null=True,blank=True)
    custname=models.CharField(u'姓名',null=True,blank=True,max_length=30)
    phoneno=models.CharField(u'联系方式',null=True,blank=True,max_length=30)
    cityname=models.CharField(u'省市区名称',null=True,blank=True,max_length=50)
    detailAdrinfo=models.CharField(u'具体地址信息',null=True,blank=True,max_length=100)
    custcont=models.CharField(u'需要对商家说的',max_length=200,null=True,blank=True)
    
    pay_noncestr=models.CharField(u'支付的随机字符串',max_length=100,null=True,blank=True)
    pay_sign=models.CharField(u'支付sign码',max_length=400,null=True,blank=True)
    pay_package=models.CharField(u'支付的package信息',max_length=400,null=True,blank=True)
    pay_ip=models.CharField(u'支付时的ip地址',max_length=40,null=True,blank=True)
    deductionitemid=models.CharField(u'支付使用的优惠券信息',max_length=40,null=True,blank=True)
    
    star=models.IntegerField(u'几星级评价',null=True,blank=True)
    Latitude=models.CharField(u'经度',max_length=50,null=True,blank=True)
    Longitude=models.CharField(u'纬度',max_length=50,null=True,blank=True)
    
    send_msg = models.BooleanField(u'是否发送短信',default=False)
    msg_content = models.CharField(u'发送短信内容',null=True,blank=True,max_length=200)
    send_at = models.DateTimeField(u'发送时间',null=True,blank=True)
    modified=models.DateTimeField(u'更新时间',auto_now=True)
    def __unicode__(self):
        return '%s' %self.id

    class Meta:
        verbose_name = u'订单'
        verbose_name_plural = '订单管理'

    def getGoodsByOrder(self):
        return OrderItem.objects.filter(order=self)

@receiver(models.signals.pre_save,sender=Order)
def myOrderPreSave(sender, instance, **kwargs):
    #保存前文件处理
    if instance.pk is not None:
        if instance.status == "已发货" and instance.logistics_order!="" :
            logisinfo=instance.logistics+"-"+instance.logistics_order
            sendlogisticsmsgtocust(instance.open_id,logisinfo,instance.id)
#         elif instance.status == "已支付" :
#             sendpayinfotocust(instance.open_id,instance.id,instance.pay_sign)
class OrderItem(models.Model):
    """
    订单项目
    """
    good=models.ForeignKey(Goods,verbose_name=u'商品')
    count=models.IntegerField(u'商品数量')
    order=models.ForeignKey(Order,verbose_name=u'订单')
    price=models.DecimalField(u'价格',max_digits=7,decimal_places=2)
    good_format=models.CharField(u'商品规格',max_length=200,null=True,blank=True)
    def __unicode__(self):
        return '%s' %self.id

    class Meta:
        verbose_name = u'订单详细'
        verbose_name_plural = '订单详细管理'
    
class Format(models.Model):
    """
             商品规格
    """
    typ_CHOICES = (('重量',u'重量'),
                   ('毫升',u'毫升'),
                   ('颜色',u'颜色'),
                   ('尺码',u'尺码'),
                   ('型号',u'型号'))
    catagory=models.CharField(u'规格种类',choices=typ_CHOICES,max_length=30)
    formatname=models.CharField(u'规格',max_length=30)
    good=models.ForeignKey(Goods,verbose_name=u'商品')
    stock=models.IntegerField(u'库存',default=0)
    price=models.DecimalField(u'价格',max_digits=7,decimal_places=2)
    def __unicode__(self):
        return '%s' %self.id

    class Meta:
        verbose_name = u'商品规格'
        verbose_name_plural = '商品规格管理'
        
class CustAssess(models.Model):
    """
             客户评价
    """
    wx_id=models.CharField(u'微信id',max_length=30)
    good=models.ForeignKey(Goods,verbose_name=u'商品')
    order=models.ForeignKey(Order,verbose_name=u'订单')
    good_grade=models.CharField(u'商品评价等级',max_length=10,blank=True)  # @UndefinedVariable
    pack_grade=models.CharField(u'包装评价等级',max_length=10,blank=True)  # @UndefinedVariable
    speed_grade=models.CharField(u'速度评价等级',max_length=10,blank=True)  # @UndefinedVariable
    dist_grade=models.CharField(u'派送评价等级',max_length=10,blank=True)  # @UndefinedVariable
    assesscont=models.CharField(u'评价内容',max_length=400,blank=True)  # @UndefinedVariable
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable
    class Meta:
        verbose_name = u"客户评价"
        verbose_name_plural = '客户评价管理'
        
class CardInfo(models.Model):
    """
                卡券
    """
    name=models.CharField(u'卡券名称',max_length=30);
    face_value=models.DecimalField(u'卡券面值',max_digits=7,decimal_places=2,default=0);
    actual_value=models.DecimalField(u'卡券充值金额',max_digits=7,decimal_places=2,default=0);
    logo=models.ImageField(u'缩略图地址',upload_to='uploads/tubiao/',null=True,blank=True)
    desc=RichTextUploadingField(u'卡券描述',max_length=1500,null=True,blank=True)
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable
    def __unicode__(self):
        return '%s' %self.name
    class Meta:
        verbose_name = u"卡券信息"
        verbose_name_plural = '卡券信息管理'
class CardItem(models.Model):
    """
                用户明细卡券
    """
    ORDER_STATUS=(
        ('已下单', u'已下单'),
        ('已取消', u'已取消'),
        ('已支付', u'已支付'),
    )
    openid=models.CharField(u'用户openid',max_length=100,null=True,blank=True)
    cardinfo=models.ForeignKey(CardInfo,verbose_name=u'卡券')
    price=models.DecimalField(u'价格',max_digits=7,decimal_places=2)
    pay_noncestr=models.CharField(u'支付的随机字符串',max_length=100,null=True,blank=True)
    pay_sign=models.CharField(u'支付sign码',max_length=400,null=True,blank=True)
    pay_package=models.CharField(u'支付的package信息',max_length=400,null=True,blank=True)
    pay_ip=models.CharField(u'支付时的ip地址',max_length=40,null=True,blank=True)
    
    status=models.CharField(u'订单状态',choices=ORDER_STATUS,default=u'已下单',max_length=100)
    
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable
    
    def __unicode__(self):
        return '%s' %self.id
    class Meta:
        verbose_name = u"卡券明细"
        verbose_name_plural = '卡券明细管理'    
# @receiver(models.signals.pre_save,sender=CardItem)
# def myCardItemPreSave(sender, instance, **kwargs):
#     #保存前文件处理
#     if instance.pk is not None:
#         if instance.status == "已支付" :
#             sendpaycardinfotocust(instance.openid,instance.price)

class CardTxn(models.Model):
    """
                卡充值明细
    """
    openid=models.CharField(u'用户openid',max_length=100,null=True,blank=True)
    card=models.CharField(u'充值卡信息',max_length=100,null=True,blank=True)
    
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable

class Deduction(models.Model):
    """
                满减抵扣券
    """
    name=models.CharField(u'抵扣券名称',max_length=30);
    condi_value=models.DecimalField(u'条件金额',max_digits=7,decimal_places=2,default=0);
    deduct_value=models.DecimalField(u'抵扣金额',max_digits=7,decimal_places=2,default=0);
    desc=RichTextUploadingField(u'卡券描述',max_length=1500,null=True,blank=True)
    logo=models.ImageField(u'缩略图地址',upload_to='uploads/tubiao/',null=True,blank=True)
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable
    def __unicode__(self):
        return '%s' %self.name
    class Meta:
        verbose_name = u"抵扣券信息"
        verbose_name_plural = '满减抵扣券管理'
    
class DeductionItem(models.Model):
    """
                 满减抵扣券明细
    """
    ORDER_STATUS=(
        ('已领取', u'已领取'),
        ('已使用', u'已使用'),
    )
    openid=models.CharField(u'用户openid',max_length=100,null=True,blank=True)
    deduction=models.ForeignKey(Deduction,verbose_name=u'满减抵扣券')
    status=models.CharField(u'抵扣券状态',choices=ORDER_STATUS,default=u'已领取',max_length=100)
    
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable
    
    def __unicode__(self):
        return '%s' %self.id
    class Meta:
        verbose_name = u"抵扣券明细"
        verbose_name_plural = '抵扣券明细管理'     

class UserFavorite(models.Model):
    """
                 用户收藏商品
    """
    openid=models.CharField(u'用户openid',max_length=100)
    good=models.ForeignKey(Goods,verbose_name=u'商品')
    
    modified=models.DateTimeField(u'更新时间',auto_now=True) # @UndefinedVariable
    def __unicode__(self):
        return '%s' %self.id
    class Meta:
        verbose_name = u"用户商品收藏夹"
        verbose_name_plural = '用户商品收藏夹管理'     
    
    
    
    
    
    
    

